<?php

class States extends Controller{

	function states(){
		parent::Controller();
		$this->load->helper('url');
    $this->load->library('session');
		$this->load->model('states_model');
	}
	/*
	function add_new()
	{
		
		$state_name = array('name'=>'state_name','id'=>'state_name','type'=>'text','class'=>'required');
		$this->load->helper('form');
		$this->load->library('form_validation');
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		
		$displayform .='<fieldset style="width:50%;"><legend>Add New State</legend>';
		$attr = array('id'=>'new_state');
		$displayform .= form_open('states/add_new',$attr);
		$displayform .= '<table >';
		$displayform .= '<tr><td align=left> State Name:</td>';
		$displayform .= '<td>'.form_input($state_name).'</td>';
		$displayform .= '</tr>';
		$displayform .= '<tr><td align=center colspan=2>'.form_submit('Submit','Create New State').'</td>';
		$displayform .= '</tr>';
		$displayform .= '</table></fieldset>';
		$displayform .=form_close();
		$this->form_validation->set_rules('state_name', 'State Name', 'trim|required|xss_clean');

		$data = array('formdisplay'=>$displayform );

		
		if ($this->form_validation->run() == FALSE)
		{		
			$this->template->add_js('js/jquery-latest.js');	
			$this->template->add_js('js/jquery.validate.js');
			
			$this->template->add_js('$(document).ready(function(){
	 		   $("#new_state").validate();
	  			});
		','embed');
			$this->template->write_view('header','header', $data, True);
			$this->template->write_view('content', 'general');
			$this->template->render();
		}
		else
		{
			
			$check=$this->states_model->insert_state();
			
			redirect('states/list_states/0');
			
		}
	}
	
	function duplicate_state()
	{
		$displayform ='';
		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
		
		$displayform .= "This place already exist,please go back and try for another name!!";
		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
		$data = array('formdisplay'=>$displayform );
		$this->template->write_view('header','header', $data, True);
		$this->template->write_view('content', 'other_general');
		$this->template->render();
	}
	*
   * 
   */
	
//to list states names	
	function list_states()
	{
		$user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
		  $this->load->helper('flexigrid');
						
	  	$displayform ='';
	  	$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
      $displayform .= '<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><span id="create-state" style="cursor:pointer;color:blue;" >Add New State</span>&nbsp;<a href="'.base_url().'idea_home/logout">Logout</a></div>';
	  	$displayform .='<div align=right></div>';
	  	$displayform .='<br/>';
	  	//here model comes....
	  	$displayform .= '<div id="dialog-form" title="Create new State">';
    $displayform .= '<p class="validateTips">State Name cannot be empty.</p>';
    $displayform .= '<form ><fieldset>';
    $displayform .= '<label class="label" for=country">Country</label>';
    $displayform .= '<input type="text" name="country" id=country value="India" class="text ui-widget-content ui-corner-all" disabled=disabled />';
    
    $displayform .= '<label for="name" class="label" >State Name</label>';
    $displayform .= '<input type="text" name="State_name" id="state_name" class="text ui-widget-content ui-corner-all" />';
    
    
    $displayform .= '</fieldset> </form>';
    $displayform .= '</div>';
    //ends here.....
    $displayform .= '<div id="display_val"></div>';
    
	  	$displayform .= '<table id="flex1" style="display:none" border=0 align=left ></table>';
	  	$displayform .= '<br/>';
		
		$colModel['state_id'] = array('Sl No',60,TRUE,'center',0);
		$colModel['state_name'] = array('State Name',160,TRUE,'left',2);
		
		$gridParams = array(
			'width' => '800',
			'height' => 400,
			'rp' => 15,
			'rpOptions' => '[10,20,30,40]',
			'pagestat' => 'Displaying:{from} to {to} of {total} items.',
			'blockOpacity' => 0.5,
			'title' => 'List of States',
			'showTableToggleBtn' => true
		);
		
		$grid_js = build_grid_js('flex1',site_url("/ajax/states_list"),$colModel,'state_id','asc',$gridParams);
		
		$data['js_grid'] = $grid_js;
			
		//model javasrcipt comes here...
		
		 $this->template->add_css('
  
    .label, #country, #state_name { display:block; }
    #state_name.text, #country.text { margin-bottom:12px; width:95%; padding: .4em; }
    fieldset { padding:0; border:0; margin-top:25px; }
    h1 { font-size: 1.2em; margin: .6em 0; }
   
    .ui-dialog .ui-state-error { padding: .3em; }
    .validateTips { border: 1px solid transparent; padding: 0.3em; }
        
    ','embed');
    
    
    $this->template->add_js('js/ui/jquery.ui.position.js');
    $this->template->add_js('js/ui/jquery.ui.mouse.js');
    $this->template->add_js('js/ui/jquery.ui.draggable.js');
    $this->template->add_js('js/ui/jquery.ui.resizable.js');
    $this->template->add_js('js/ui/jquery.ui.dialog.js');
    $this->template->add_js('js/ui/jquery.effects.core.js');
    $this->template->add_js('
     $(function() {
   
    $( "#dialog:ui-dialog" ).dialog( "destroy" );
    
    var name = $( "#state_name" ),
     
      allFields = $( [] ).add( name ),
      tips = $( ".validateTips" );

    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
        return false;
      } else {
        return true;
      }
    }
    
    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }   
       
    $( "#dialog-form" ).dialog({
      autoOpen: false,
      height:400,
      width: 450,
      modal: true,
      buttons: {
        "Create New State": function() {
          var bValid = true;
          allFields.removeClass( "ui-state-error" );

          bValid = bValid && checkLength( name, "State Name", 3, 40 );
          bValid = bValid && checkRegexp( name, /^[a-z]([a-z ])+$/i, "State Name may consist of a-z, begin with a letter." );
          
      
          if ( bValid ) {
                                 
              $.ajax({
                type: "GET",
                url: "/states/ajax_add_new/"+ name.val(),
          
                dataType: "text/html",
                success: function(html){       $("#display_val").html(html);     }
          
                  });
            
            $( this ).dialog( "close" );
          }
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },
      close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
      }
    });

    $( "#create-state" )
     
      .click(function() {
        $( "#dialog-form" ).dialog( "open" );
      });
  });
     

    ','embed');
    	
			
			
			
			
		$data = array('formdisplay'=>$displayform,'js_grid'=>$grid_js );	
		$this->template->add_css('css/flexigrid/flexigrid.css');
		//$this->template->add_js('js/flexigride/jquery.pack.js');
		$this->template->add_js('js/flexigride/flexigrid.pack.js');
		
		//ends here....
		$this->template->write_view('header','header', $data, True);
		$this->template->write_view('content', 'other_general');
		$this->template->render();
		}
else
  {
    echo "Forbidden to access";
  }
	  				
		
	}
	
		
	function state_edit($id=0)
	{
		$user_ses = $this->session->userdata('userlogindetails');
 
    if($user_ses['user_id'] && $user_ses['user_group']==1)
    {
  		$this->load->helper('form');
  		$this->load->library('form_validation');
  	
  		$state_name = $this->states_model->state_edit($id);
  		$name = $state_name->result_array();
  		$state_name = array('name'=>'state_name','value'=>$name[0]['state_name'],'type'=>'text','class'=>'required');//its already posted so it will not set again here after validati
  			
  		
  		$this->form_validation->set_rules('state_name', 'State Name', 'trim|required|xss_clean');
  		$displayform ='';
  		$displayform .='<div id=dashboard ><span>Manage State</span> <a href='.base_url().'/idea_home/admin_dashboard ><img align=right src="'.base_url().'images/home.png" alt="home" /></a><img align=right src="'.base_url().'images/back.png" alt="back" id=back ONCLICK="history.go(-1)" /></div>';
  		$displayform .='<span style="float:left;">Welcome '.$user_ses['user_name'].'</span><div id=logout><a href="'.base_url().'idea_home/logout">Logout</a></div><br/>';
  		$displayform .='<fieldset style="width:50%;"><legend>Edit State Name</legend>';
  		$attr = array('id'=>'edit_state');
  		$displayform .= form_open('states/state_edit',$attr);
  		$displayform .= '<input type=hidden name=id value='.$id.' />';
  		$displayform .= '<table class="tablestyle">';
  		$displayform .= '<tr><td align=left> State Name:</td>';
  		$displayform .= '<td>'.form_input($state_name).'</td>';
  		$displayform .= '</tr>';
  		
  		$displayform .= '<tr><td align=center colspan=2>'.form_submit('submit','Save').'</td>';
  		$displayform .= '</tr>';
  		$displayform .= '</table></fieldset>';
  		$displayform .=form_close();
  		//$displayform .= '<div align="right"><input type=button value="Go Back" ONCLICK="history.go(-1)" /></div>';
  		
  		if ($this->form_validation->run() == FALSE)
  		{
  			$data = array('formdisplay'=>$displayform );
  			$this->template->add_js('js/jquery-latest.js');	
  			$this->template->add_js('js/jquery.validate.js');
  			$this->template->add_js('js/validate_xls.js');
  			$this->template->add_js('$(document).ready(function(){
  	 		   $("#edit_state").validate();
  	  			});
  		','embed');
  			$this->template->write_view('header','header', $data, True);
  			$this->template->write_view('content', 'general');
  			$this->template->render();
  		}
  		else
  		{
  			$this->states_model->save_edit();
  			redirect('states/list_states/0');
  			
  		}
 		}
else
  {
    echo "Forbidden to Access!!";
  }
		
	}
	
  
  //to add to new state information to db using ajax...
  function ajax_add_new($str = '')
  {
      $org_str = str_ireplace("_", " ", $str);
    
      $check=$this->states_model->insert_state($org_str);
      
      if($check == 0)
      {
          echo "<p style='color:red;'> The state name you're trying insert is already exist!!! </p>";
      }
     
  }
}
	
 
